<!DOCTYPE html>
<html xmlns:th="http://www.springframework.org/schema/data/jaxb">
<head>
    <meta charset="utf-8">
    <title>系统用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"  content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/search.css}" media="all">
    <script language=javascript th:src='@{/js/jquery-3.4.1.js}'></script>
    <script th:src="@{/layuiadmin/layui/layui.js}"></script>
    <script th:src="@{/js/app.js}"></script>
</head>
<body>
<style>

</style>
<!-- 搜索 -->
<div class="searchTable">
    搜索ID：
    <div class="layui-inline">
        <input class="layui-input" id="id" name="id" autocomplete="off">
    </div>
    <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="reload">搜索</button>
</div>
<!-- 表格 -->
<table class="auth-table" id="auth-table" lay-filter="auth-table"></table>
<!-- 多删除添加  -->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="del">删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>
<!-- 每一行的数据修改或删除  -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>

    let findAll = ADMIN_CONSOLE + "/userAdmin/findAll/1";      //用户查询
    let deletes = ADMIN_CONSOLE + "/userAdmin/delete";         //用户删除
    let pageUpd = ADMIN_CONSOLE + "/page/console_user_userUpd";  //用户编辑弹出
    let pageAdd = ADMIN_CONSOLE + "/page/console_user_userAdd";  //用户添加弹出

    layui.use('table', function () {
        let table = layui.table;
        table.render({
            elem: '#auth-table'
            , url: findAll
            , toolbar: '#toolbarDemo'
            , title: '用户数据表'
            ,parseData: function(res){ //res 即为原始返回的数据
                // return {
                //     "code": res.status, //解析接口状态
                //     "msg": res.message, //解析提示文本
                //     "count": res.total, //解析数据长度
                //     "data": res.data.item //解析数据列表
                // };
                console.log(res.code);
                console.log(JSON.stringify(res));
            }
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'id', title: 'ID', width: 80, fixed: 'left'}
                    , {field: 'head', title: '头像'}
                    , {field: 'username', title: '用户名'}
                    , {field: 'account', title: '账号'}
                    , {field: 'password', title: '密码'}
                    , {field: 'age', title: '年龄'}
                    , {field: 'gender', title: '性别'}
                    , {field: 'address', title: '地址'}
                    , {field: 'empId', title: '部门'}
                    , {field: 'time', title: '注册时间', width: 250}
                    , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 250}
                ]
            ]
            , page: pageJson
        });


        /**
         * 工具栏事件（多删除 || 添加）
         * toolbar = script的 id
         * auth-table = 数据表格id/lay-filter
         */
        table.on('toolbar(auth-table)', function (obj) {
            let checkStatus = table.checkStatus(obj.config.id);
            //多删除
            if (obj.event === "del") {
                let ids = new Array();
                let data = checkStatus.data;
                for (let i = 0; i < data.length; i++) {
                    ids[i] = data[i].id;
                }
                if(ids.length > 0){
                    ajaxPost(deletes,{ids : ids});
                    //执行重载
                    table.reload('auth-table', {
                        page: {
                            curr: $(".layui-laypage-skip .layui-input").val() //页数
                        }
                    }, 'data');
                }
            }
            //添加
            if (obj.event === "add") {
               tipsWindown(pageAdd,"600px","400px","用户添加");
            }
        });


        /**
         * 条件搜索，重载数据
         */
        let $ = layui.$, active = {
            //搜索
            reload: function () {
                table.reload('auth-table', {
                    page: {
                        curr: 1  //页数
                    }
                    , where: {
                        id: $("#id").val()
                    }
                }, 'data');
            }
        };
        $('.searchTable .layui-btn').on('click', function () {
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        /**
         * 监听数据表格内按钮（编辑 || 删除）
         * tool = 表格内按钮
         * auth-table =表格名
         */
        table.on('tool(auth-table)', function (obj) {
            data = obj.data;  //当前行数据
            //菜单添加
            if (obj.event === 'edit') {
                tipsWindown(pageUpd,"600px","400px","用户编辑");
            }
            //删除
            if (obj.event === 'del') {
               tipsDelete(deletes,{ids : [data.id]},obj);
            }
        });
    });
    //当前行数据记录,弹出层获取回填值
    var data = "";
</script>
</body>
</html>